package com.wy;


import com.wy.utils.EncodingWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class EncondingFilter implements Filter {
    public static String encoding = "UTF-8";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强转
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if ("GET".equalsIgnoreCase(request.getMethod())) {
            //get请求
            EncodingWrapper wrapper = new EncodingWrapper(request, encoding);
            filterChain.doFilter(wrapper, response);
        } else {
            //post请求
            request.setCharacterEncoding(encoding);
            response.setContentType("text/html;charset=" + encoding);
            filterChain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
